<!doctype html>



  


<html class="theme-next mist use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="react," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.1" />






<meta name="description" content="学习资源
原版官网，中文版官网。
阮一峰的微博。
菜鸟网。
徐东亮老师的reactJS从入门到实践。
代码转换Babel，例如jsx转为js 
另一个入门教程网站。知识点样式React 推荐使用内联样式，注：不用加单位值1234567891011var i=1;var myStyle=&amp;#123;  fontSize:100,color:&apos;green&apos;&amp;#125;;ReactDOM.render(">
<meta property="og:type" content="article">
<meta property="og:title" content="学习rect">
<meta property="og:url" content="http://YoungHearts.oschina.io/20160607/rect/index.html">
<meta property="og:site_name" content="YangJ's Blog">
<meta property="og:description" content="学习资源
原版官网，中文版官网。
阮一峰的微博。
菜鸟网。
徐东亮老师的reactJS从入门到实践。
代码转换Babel，例如jsx转为js 
另一个入门教程网站。知识点样式React 推荐使用内联样式，注：不用加单位值1234567891011var i=1;var myStyle=&amp;#123;  fontSize:100,color:&apos;green&apos;&amp;#125;;ReactDOM.render(">
<meta property="og:image" content="http://YoungHearts.oschina.io/images/bebel.png">
<meta property="og:updated_time" content="2016-06-14T02:17:08.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="学习rect">
<meta name="twitter:description" content="学习资源
原版官网，中文版官网。
阮一峰的微博。
菜鸟网。
徐东亮老师的reactJS从入门到实践。
代码转换Babel，例如jsx转为js 
另一个入门教程网站。知识点样式React 推荐使用内联样式，注：不用加单位值1234567891011var i=1;var myStyle=&amp;#123;  fontSize:100,color:&apos;green&apos;&amp;#125;;ReactDOM.render(">
<meta name="twitter:image" content="http://YoungHearts.oschina.io/images/bebel.png">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Mist',
    sidebar: {"position":"left","display":"always"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: 0,
      author: '博主'
    }
  };
</script>

  <title> 学习rect | YangJ's Blog </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  



  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?783106e6f997bc237c1e1b160cedbe88";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>








  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">YangJ's Blog</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">记录我的学习工作点滴</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="#" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>


    </div>
  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                学习rect
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time itemprop="dateCreated" datetime="2016-06-07T15:51:23+08:00" content="2016-06-07">
              2016-06-07
            </time>
          </span>

          

          
            
              <span class="post-comments-count">
                &nbsp; | &nbsp;
                <a href="/20160607/rect/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="20160607/rect/" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          

          
          
             <span id="/20160607/rect/" class="leancloud_visitors" data-flag-title="学习rect">
               &nbsp; | &nbsp;
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               <span class="post-meta-item-text">阅读次数 </span>
               <span class="leancloud-visitors-count"></span>
              </span>
          

          
              &nbsp; | &nbsp;
              <span class="page-pv"><i class="fa fa-file-o"></i>
              <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>
              </span>
          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="学习资源"><a href="#学习资源" class="headerlink" title="学习资源"></a>学习资源</h3><ol>
<li><a href="https://facebook.github.io/react/index.html" target="_blank" rel="external">原版官网</a>，<a href="http://reactjs.cn/" target="_blank" rel="external">中文版官网</a>。</li>
<li><a href="http://www.ruanyifeng.com/blog/2015/03/react.html" target="_blank" rel="external">阮一峰的微博</a>。</li>
<li><a href="http://www.runoob.com/react/react-jsx.html" target="_blank" rel="external">菜鸟网</a>。</li>
<li>徐东亮老师的reactJS从入门到实践。</li>
<li>代码转换<a href="https://babeljs.io/" target="_blank" rel="external">Babel</a>，例如jsx转为js <img src="/images/bebel.png" alt="jsx转为js"></li>
<li><a href="https://hulufei.gitbooks.io/react-tutorial/content/" target="_blank" rel="external">另一个入门教程网站</a>。<h3 id="知识点"><a href="#知识点" class="headerlink" title="知识点"></a>知识点</h3><h4 id="样式"><a href="#样式" class="headerlink" title="样式"></a>样式</h4>React 推荐使用内联样式，注：不用加单位值<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">var i=1;</div><div class="line">var myStyle=&#123;</div><div class="line">  fontSize:100,color:<span class="string">'green'</span></div><div class="line">&#125;;</div><div class="line">ReactDOM.render(</div><div class="line">        &lt;div&gt;</div><div class="line">            &lt;h1 style=&#123;myStyle&#125;&gt;&#123;i == 1 ? <span class="string">'对的'</span> : <span class="string">'不对'</span>&#125;&lt;/h1&gt;</div><div class="line">        &lt;/div&gt;</div><div class="line">        ,</div><div class="line">        document.getElementById(<span class="string">'example'</span>)</div><div class="line">);</div></pre></td></tr></table></figure>
</li>
</ol>
<h4 id="数组"><a href="#数组" class="headerlink" title="数组"></a>数组</h4><p>JSX 允许在模板中插入数组，数组会自动展开所有成员，且注意，若有多个组件，那么只能有一个最外层元素包裹，否则无法执行：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line">var i=1;</div><div class="line">var myStyle=&#123;</div><div class="line">  fontSize:100,color:<span class="string">'green'</span></div><div class="line">&#125;;</div><div class="line">var arr=[</div><div class="line">        &lt;h1&gt;哥学的不仅是技术&lt;/h1&gt;,</div><div class="line">        &lt;h3&gt;还是寂寞&lt;/h3&gt;,</div><div class="line">];</div><div class="line">ReactDOM.render(</div><div class="line">        &lt;div&gt;</div><div class="line">            &lt;h1 style=&#123;myStyle&#125;&gt;&#123;i == 1 ? <span class="string">'True!'</span> : <span class="string">'False'</span>&#125;&lt;/h1&gt;</div><div class="line">            &lt;div&gt;&#123;arr&#125;&lt;/div&gt;</div><div class="line">        &lt;/div&gt;</div><div class="line">        ,</div><div class="line">        document.getElementById(<span class="string">'example'</span>)</div><div class="line">);</div></pre></td></tr></table></figure></p>
<h4 id="组件"><a href="#组件" class="headerlink" title="组件"></a>组件</h4><p>react就是利用一个个小组件的拼接</p>
<h5 id="HelloMessage"><a href="#HelloMessage" class="headerlink" title="HelloMessage"></a>HelloMessage</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">var HelloMessage=React.createClass(&#123;</div><div class="line">    render:<span class="function"><span class="title">function</span></span>()&#123;</div><div class="line">        <span class="built_in">return</span> &lt;h1&gt;你好,世界!&lt;/h1&gt;;</div><div class="line">    &#125;</div><div class="line">&#125;);</div><div class="line">ReactDOM.render(</div><div class="line">        &lt;HelloMessage/&gt;,</div><div class="line">        document.getElementById(<span class="string">'example'</span>)</div><div class="line">);//HelloMessage</div></pre></td></tr></table></figure>
<p><em>注意，原生 HTML 元素名以小写字母开头，而自定义的 React 类名以大写字母开头，比如 HelloMessage 不能写成 helloMessage。除此之外还需要注意组件类只能包含一个顶层标签，否则也会报错。</em></p>
<h5 id="this-props"><a href="#this-props" class="headerlink" title="this.props"></a>this.props</h5><p>如果我们需要向组件传递参数，可以使用 <code>this.props</code> 对象,实例如下：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">var HelloMessage=React.createClass(&#123;</div><div class="line">    render:<span class="function"><span class="title">function</span></span>()&#123;</div><div class="line">        <span class="built_in">return</span> &lt;h1&gt;我的博客是&#123;this.props.name&#125;&lt;/h1&gt;;</div><div class="line">    &#125;</div><div class="line">&#125;);</div><div class="line">ReactDOM.render(</div><div class="line">        &lt;HelloMessage name=<span class="string">"younghearts.oschina.io"</span>/&gt;,</div><div class="line">        document.getElementById(<span class="string">'example'</span>)</div><div class="line">);//this.props</div></pre></td></tr></table></figure></p>
<h5 id="复合组件"><a href="#复合组件" class="headerlink" title="复合组件"></a>复合组件</h5><p>我们可以通过创建多个组件来合成一个组件，即把组件的不同功能点进行分离。<br>以下实例我们实现了输出网站名字和网址的组件：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line">var Name=React.createClass(&#123;</div><div class="line">    render:<span class="function"><span class="title">function</span></span>()&#123;</div><div class="line">        <span class="built_in">return</span> (</div><div class="line">                &lt;h1&gt;&#123;this.props.name&#125;&lt;/h1&gt;</div><div class="line">        )</div><div class="line">    &#125;</div><div class="line">&#125;);//组件1.name</div><div class="line">var Link=React.createClass(&#123;</div><div class="line">    render:<span class="function"><span class="title">function</span></span>()&#123;</div><div class="line">        <span class="built_in">return</span> (</div><div class="line">                &lt;a href=&#123;this.props.site&#125;&gt;&#123;this.props.site&#125;&lt;/a&gt;</div><div class="line">        )</div><div class="line">    &#125;</div><div class="line">&#125;);//组件2.Link</div><div class="line">var WebSite=React.createClass(&#123;</div><div class="line">    render:<span class="function"><span class="title">function</span></span>()&#123;</div><div class="line">        <span class="built_in">return</span> (</div><div class="line">                &lt;div&gt;</div><div class="line">                    &lt;Name name=&#123;this.props.name&#125;/&gt;</div><div class="line">                    &lt;Link site=&#123;this.props.site&#125;/&gt;</div><div class="line">                &lt;/div&gt;</div><div class="line">        )</div><div class="line">    &#125;</div><div class="line">&#125;);//复合组件WebSite(1+2), WebSite 组件使用了 Name 和 Link 组件来输出信息</div><div class="line">ReactDOM.render(</div><div class="line">        &lt;WebSite name=<span class="string">"杨键的博客"</span> site=<span class="string">"http://younghearts.oschina.io"</span>/&gt;,</div><div class="line">        document.getElementById(<span class="string">'example'</span>)</div><div class="line">)//渲染输出</div></pre></td></tr></table></figure></p>
<h5 id="state"><a href="#state" class="headerlink" title="state"></a>state</h5><p>两个案例:<br>getInitialState是用来初始化state，handleClick是用来处理我们点击事件的。<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div></pre></td><td class="code"><pre><div class="line">/*var LinkButton=React.createClass(&#123;</div><div class="line">    getInitialState:<span class="function"><span class="title">function</span></span> () &#123;</div><div class="line">        <span class="built_in">return</span>&#123;linked:<span class="literal">false</span>&#125;;//设置初始状态为<span class="literal">false</span></div><div class="line">    &#125;,</div><div class="line">    handleClick:<span class="keyword">function</span>(event)&#123;//设置自定义的handleClick方法</div><div class="line">        this.setState(&#123;linked:!this.state.linked&#125;);</div><div class="line">    &#125;,</div><div class="line">    render:<span class="function"><span class="title">function</span></span> () &#123;</div><div class="line">        var text=this.state.linked?<span class="string">'喜欢'</span>:<span class="string">'不喜欢'</span>;</div><div class="line">        <span class="built_in">return</span> (</div><div class="line">                &lt;p onClick=&#123;this.handleClick&#125;&gt;</div><div class="line">                    你&lt;b&gt;&#123;text&#125;&lt;/b&gt;我.点击切换状态</div><div class="line">                &lt;/p&gt;</div><div class="line">        )</div><div class="line">    &#125;</div><div class="line">&#125;)</div><div class="line">React.render(</div><div class="line">        &lt;LinkButton/&gt;,</div><div class="line">        document.getElementById(<span class="string">'example'</span>)</div><div class="line">)//渲染输出*/</div><div class="line"></div><div class="line">var ColorButton = React.createClass(&#123;</div><div class="line">    getInitialState: <span class="function"><span class="title">function</span></span> () &#123;</div><div class="line">        <span class="built_in">return</span> &#123;bColor: <span class="string">'red'</span>&#125;;//设置初始状态为<span class="literal">false</span></div><div class="line">    &#125;,</div><div class="line">    handleClick: <span class="keyword">function</span> (event) &#123;//设置自定义的handleClick方法</div><div class="line">        this.setState(&#123;bColor: this.state.bColor === <span class="string">'green'</span> ? <span class="string">'red'</span> : <span class="string">'green'</span>&#125;);</div><div class="line">    &#125;,</div><div class="line">    render: <span class="function"><span class="title">function</span></span> () &#123;</div><div class="line">        <span class="built_in">return</span> (</div><div class="line">                &lt;button onClick =&#123;this.handleClick&#125; style=&#123;&#123;backgroundColor:this.state.bColor&#125;&#125;&gt;点我&lt;/button&gt;</div><div class="line">        )</div><div class="line">    &#125;</div><div class="line">&#125;)</div><div class="line">ReactDOM.render(</div><div class="line">        &lt;ColorButton/&gt;,</div><div class="line">        document.getElementById(<span class="string">'example'</span>)</div><div class="line">)//渲染输出</div></pre></td></tr></table></figure></p>
<h5 id="Props"><a href="#Props" class="headerlink" title="Props"></a>Props</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div></pre></td><td class="code"><pre><div class="line">var HelloMessage = React.createClass(&#123;</div><div class="line">    render: <span class="function"><span class="title">function</span></span> () &#123;</div><div class="line">        <span class="built_in">return</span> &lt;h1&gt;Hello&#123;this.props.name&#125;&lt;/h1&gt;</div><div class="line">    &#125;</div><div class="line">&#125;);</div><div class="line">ReactDOM.render(</div><div class="line">        &lt;HelloMessage name=<span class="string">' yj'</span>/&gt;,</div><div class="line">        document.getElementById(<span class="string">'example'</span>)</div><div class="line">)//渲染输出</div><div class="line"></div><div class="line">//通过 getDefaultProps() 方法为 props 设置默认值</div><div class="line">var HelloMessage = React.createClass(&#123;</div><div class="line">    render: <span class="function"><span class="title">function</span></span> () &#123;</div><div class="line">        <span class="built_in">return</span> &lt;h1&gt;Hello&#123;this.props.name&#125;&lt;/h1&gt;</div><div class="line">    &#125;,</div><div class="line">    getDefaultProps: <span class="function"><span class="title">function</span></span> () &#123;</div><div class="line">        <span class="built_in">return</span> &#123;</div><div class="line">            name: <span class="string">'yj'</span></div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;);</div><div class="line">ReactDOM.render(</div><div class="line">        &lt;HelloMessage/&gt;,</div><div class="line">        document.getElementById(<span class="string">'example'</span>)</div><div class="line">)//渲染输出</div></pre></td></tr></table></figure>
<h4 id="State-和-Props"><a href="#State-和-Props" class="headerlink" title="State 和 Props"></a>State 和 Props</h4><p>区别在于 props 是不可变的，而 state 可以根据与用户交互来改变。这就是为什么有些容器组件需要定义 state 来更新和修改数据。 而子组件只能通过 state 来传递数据。<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div></pre></td><td class="code"><pre><div class="line">var WebSite = React.createClass(&#123;</div><div class="line">    getInitialState: <span class="function"><span class="title">function</span></span> () &#123;</div><div class="line">        <span class="built_in">return</span> &#123;</div><div class="line">            name: <span class="string">"杨键"</span>,</div><div class="line">            site: <span class="string">"http://younghearts.oschina.io"</span></div><div class="line">        &#125;;</div><div class="line">    &#125;,</div><div class="line">    render: <span class="function"><span class="title">function</span></span> () &#123;</div><div class="line">        <span class="built_in">return</span> (</div><div class="line">                &lt;div&gt;</div><div class="line">                    &lt;Name name=&#123;this.state.name&#125;/&gt;</div><div class="line">                    &lt;Link site=&#123;this.state.site&#125;/&gt;</div><div class="line">                &lt;/div&gt;</div><div class="line">        )</div><div class="line">    &#125;</div><div class="line">&#125;);//父组件中设置state</div><div class="line"></div><div class="line">var Name = React.createClass(&#123;</div><div class="line">    render: <span class="function"><span class="title">function</span></span> () &#123;</div><div class="line">        <span class="built_in">return</span> (</div><div class="line">                &lt;h1&gt;&#123;this.props.name&#125;&lt;/h1&gt;</div><div class="line">        )</div><div class="line">    &#125;</div><div class="line">&#125;);//子组件中设置props</div><div class="line">var Link = React.createClass(&#123;</div><div class="line">    render: <span class="function"><span class="title">function</span></span> () &#123;</div><div class="line">        <span class="built_in">return</span> (</div><div class="line">                &lt;a href=&#123;this.props.site&#125;&gt;</div><div class="line">                    &#123;this.props.site&#125;</div><div class="line">                &lt;/a&gt;</div><div class="line">        )</div><div class="line">    &#125;</div><div class="line">&#125;);//子组件中设置props</div><div class="line">ReactDOM.render(</div><div class="line">        &lt;WebSite/&gt;,</div><div class="line">        document.getElementById(<span class="string">'example'</span>)</div><div class="line">)</div></pre></td></tr></table></figure></p>

      
    </div>

    <div>
      
        
      
    </div>

    <div>
      
        
  <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
    <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
    <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
      <span>赏</span>
    </button>
    <div id="QR" style="display: none;">
      
        <div id="wechat" style="display: inline-block">
          <img id="wechat_qr" src="/uploads/wechatpay.jpg" alt="YangJ WeChat Pay"/>
          <p>微信打赏</p>
        </div>
      
      
    </div>
  </div>


      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/react/" rel="tag">#react</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/20160603/搭建hexo的坑/" rel="next" title="搭建hexo">
                <i class="fa fa-chevron-left"></i> 搭建hexo
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/20160614/组件的生命周期/" rel="prev" title="react的组件的生命周期">
                react的组件的生命周期 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
        <!-- JiaThis Button BEGIN -->
<div class="jiathis_style">
  <a class="jiathis_button_tsina"></a>
  <a class="jiathis_button_tqq"></a>
  <a class="jiathis_button_weixin"></a>
  <a class="jiathis_button_cqq"></a>
  <a class="jiathis_button_douban"></a>
  <a class="jiathis_button_renren"></a>
  <a class="jiathis_button_qzone"></a>
  <a class="jiathis_button_kaixin001"></a>
  <a class="jiathis_button_copy"></a>
  <a href="http://www.jiathis.com/share" class="jiathis jiathis_txt jiathis_separator jtico jtico_jiathis" target="_blank"></a>
  <a class="jiathis_counter_style"></a>
</div>
<script type="text/javascript" >
  var jiathis_config={
    hideMore:false
  }
</script>
<script type="text/javascript" src="http://v3.jiathis.com/code/jia.js" charset="utf-8"></script>
<!-- JiaThis Button END -->

      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="20160607/rect/"
           data-title="学习rect" data-url="http://YoungHearts.oschina.io/20160607/rect/">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/uploads/avatar.jpg"
               alt="YangJ" />
          <p class="site-author-name" itemprop="name">YangJ</p>
          <p class="site-description motion-element" itemprop="description">欢迎访问我的个人网站，望大家互相交流学习，有问题可在文章下方留言。</p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">13</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">10</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/YoungHearts" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="uploads/FullSizeRender.jpg" target="_blank" title="微信">
                  
                    <i class="fa fa-fw fa-weixin"></i>
                  
                  微信
                </a>
              </span>
            
          
        </div>

        
        

        
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#学习资源"><span class="nav-number">1.</span> <span class="nav-text">学习资源</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#知识点"><span class="nav-number">2.</span> <span class="nav-text">知识点</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#样式"><span class="nav-number">2.1.</span> <span class="nav-text">样式</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#数组"><span class="nav-number">2.2.</span> <span class="nav-text">数组</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#组件"><span class="nav-number">2.3.</span> <span class="nav-text">组件</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#HelloMessage"><span class="nav-number">2.3.1.</span> <span class="nav-text">HelloMessage</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#this-props"><span class="nav-number">2.3.2.</span> <span class="nav-text">this.props</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#复合组件"><span class="nav-number">2.3.3.</span> <span class="nav-text">复合组件</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#state"><span class="nav-number">2.3.4.</span> <span class="nav-text">state</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#Props"><span class="nav-number">2.3.5.</span> <span class="nav-text">Props</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#State-和-Props"><span class="nav-number">2.4.</span> <span class="nav-text">State 和 Props</span></a></li></ol></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy;  2015 - 
  <span itemprop="copyrightYear">2016</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">YangJ</span>
</div>

<div class="powered-by">
  powered-by <a class="theme-link" href="http://hexo.io">Hexo</a>
</div>

<div class="theme-info">
  Tm -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>

        

<div class="busuanzi-count">

  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv"><i class="fa fa-user"></i><span class="busuanzi-value" id="busuanzi_value_site_uv"></span></span>
  

  
    <span class="site-pv"><i class="fa fa-eye"></i><span class="busuanzi-value" id="busuanzi_value_site_pv"></span></span>
  
  
</div>



        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/vendors/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/vendors/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/vendors/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.1"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"yangjos"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
      
      <script src="/vendors/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
      <script src="/js/src/hook-duoshuo.js"></script>
    
  






  
  
  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length == 0) {
       search_path = "search.xml";
    }
    var path = "/" + search_path;
    // monitor main search box;

    function proceedsearch() {
      $("body").append('<div class="popoverlay">').css('overflow', 'hidden');
      $('.popup').toggle();

    }
    // search function;
    var searchFunc = function(path, search_id, content_id) {
    'use strict';
    $.ajax({
        url: path,
        dataType: "xml",
        async: true,
        success: function( xmlResponse ) {
            // get the contents from search data
            isfetched = true;
            $('.popup').detach().appendTo('.header-inner');
            var datas = $( "entry", xmlResponse ).map(function() {
                return {
                    title: $( "title", this ).text(),
                    content: $("content",this).text(),
                    url: $( "url" , this).text()
                };
            }).get();
            var $input = document.getElementById(search_id);
            var $resultContent = document.getElementById(content_id);
            $input.addEventListener('input', function(){
                var matchcounts = 0;
                var str='<ul class=\"search-result-list\">';                
                var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                $resultContent.innerHTML = "";
                if (this.value.trim().length > 1) {
                // perform local searching
                datas.forEach(function(data) {
                    var isMatch = true;
                    var content_index = [];
                    var data_title = data.title.trim().toLowerCase();
                    var data_content = data.content.trim().replace(/<[^>]+>/g,"").toLowerCase();
                    var data_url = data.url;
                    var index_title = -1;
                    var index_content = -1;
                    var first_occur = -1;
                    // only match artiles with not empty titles and contents
                    if(data_title != '' && data_content != '') {
                        keywords.forEach(function(keyword, i) {
                            index_title = data_title.indexOf(keyword);
                            index_content = data_content.indexOf(keyword);
                            if( index_title < 0 && index_content < 0 ){
                                isMatch = false;
                            } else {
                                if (index_content < 0) {
                                    index_content = 0;
                                }
                                if (i == 0) {
                                    first_occur = index_content;
                                }
                            }
                        });
                    }
                    // show search results
                    if (isMatch) {
                        matchcounts += 1;
                        str += "<li><a href='"+ data_url +"' class='search-result-title'>"+ data_title +"</a>";
                        var content = data.content.trim().replace(/<[^>]+>/g,"");
                        if (first_occur >= 0) {
                            // cut out 100 characters
                            var start = first_occur - 20;
                            var end = first_occur + 80;
                            if(start < 0){
                                start = 0;
                            }
                            if(start == 0){
                                end = 50;
                            }
                            if(end > content.length){
                                end = content.length;
                            }
                            var match_content = content.substring(start, end);
                            // highlight all keywords
                            keywords.forEach(function(keyword){
                                var regS = new RegExp(keyword, "gi");
                                match_content = match_content.replace(regS, "<b class=\"search-keyword\">"+keyword+"</b>");
                            });
                            
                            str += "<p class=\"search-result\">" + match_content +"...</p>"
                        }
                        str += "</li>";
                    }
                })};
                str += "</ul>";
                if (matchcounts == 0) { str = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>' }
                if (keywords == "") { str = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>' }
                $resultContent.innerHTML = str;
            });
            proceedsearch();
        }
    });}

    // handle and trigger popup window;
    $('.popup-trigger').mousedown(function(e) {
      e.stopPropagation();
      if (isfetched == false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };

    });

    $('.popup-btn-close').click(function(e){
      $('.popup').hide();
      $(".popoverlay").remove();
      $('body').css('overflow', '');
    });
    $('.popup').click(function(e){
      e.stopPropagation();
    });
  </script>

  

  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
  <script>AV.initialize("0LW1BYEc5igbSTxyRLe6JS8n-gzGzoHsz", "vFsj134Nzu8lKK1s1kLPs4y3");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  
<script type="text/javascript" async src="//push.zhanzhang.baidu.com/push.js">
</script>


</body>
</html>
